package com.angus.modules.front.repository;

import com.angus.api.front.domain.FrontLoginInfo;
import com.angus.modules.front.mapper.FrontLoginInfoMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.sql.Date;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.List;
import java.util.Map;

/**
 * @author liyanan
 * @date 2024/10/18 3:53 PM
 * @description
 */
@Repository
public class FrontLoginInfoRepository extends ServiceImpl<FrontLoginInfoMapper, FrontLoginInfo> {

    public List<FrontLoginInfo> selectLoginInfoList(String ipaddr, String userName,
                                                    String beginTime, String endTime
                                                    ) {

        LambdaQueryWrapper<FrontLoginInfo> queryWrapper = Wrappers.lambdaQuery(FrontLoginInfo.class);
        if(StringUtils.isNotBlank(ipaddr)){
            queryWrapper.like(FrontLoginInfo::getIpaddr,ipaddr);
        }
        if(StringUtils.isNotBlank(userName)){
            queryWrapper.like(FrontLoginInfo::getUserName,userName);
        }
        if(StringUtils.isNotBlank(beginTime)){
            queryWrapper.ge(FrontLoginInfo::getAccessTime,beginTime);
        }
        if (StringUtils.isNotBlank(endTime)){
            queryWrapper.le(FrontLoginInfo::getAccessTime,endTime);
        }
        queryWrapper.orderByDesc(FrontLoginInfo::getInfoId);
        return baseMapper.selectList(queryWrapper);
    }


    public int insertLoginInfo(FrontLoginInfo loginInfo){
        loginInfo.setAccessTime(Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant()));
        return  baseMapper.insert(loginInfo);
    }


    public int deleteLoginInfoByIds(List<Long> infoIds) {

       return baseMapper.deleteBatchIds(infoIds);
    }

    public void cleanLoginInfo() {
        baseMapper.cleanLoginInfo();
    }
}
